package com.yl.immodule.fragment

import android.content.Context
import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.android.yl.ylicpsdk.ICPRtcEventHandler
import com.android.yl.ylicpsdk.ICPRtcManager
import com.hm.poc.IPocEngineEventHandler
import com.unionbroad.greendao.MessageDialogue
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AccstateModel @Inject constructor(
    private val context: Context?
): ViewModel() {
    
    private val _userName = MutableLiveData<String>()
    val userName: LiveData<String> = _userName
    
    private val _state = MutableLiveData<String>()
    val state: LiveData<String> = _state

    val channelState: MutableLiveData<String> by lazy {
        MutableLiveData<String>("待加入对讲组")
    }
    // 连接状态枚举，用于Fragment判断显示哪个drawable
    enum class ConnectionState {
        CONNECTED,    // 已连接 - 绿色
        CONNECTING,   // 连接中 - 黄色  
        DISCONNECTED  // 未连接 - 灰色
    }
    
    private val _connectionState = MutableLiveData<ConnectionState>()
    val connectionState: LiveData<ConnectionState> = _connectionState

    private var mEventHandler: ICPRtcEventHandler? = null

    init {
        if (ICPRtcManager.getsInstance().getCurrentUser() != null) {
            _userName.value = ICPRtcManager.getsInstance().getCurrentUser().getName()
        }
        
        val stateA: Int = ICPRtcManager.getsInstance().getServiceState()
        when (stateA) {
            IPocEngineEventHandler.ServiceState.STATE_CONNECTED -> {
                _state.value = "已连接"
                _connectionState.value = ConnectionState.CONNECTED
            }
            IPocEngineEventHandler.ServiceState.STATE_CONNECTING -> {
                _state.value = "连接中"
                _connectionState.value = ConnectionState.CONNECTING
            }
            IPocEngineEventHandler.ServiceState.STATE_DISCONNECTED -> {
                _state.value = "未连接"
                _connectionState.value = ConnectionState.DISCONNECTED
            }
        }

        mEventHandler = aICPRtcEventHandler()
        ICPRtcManager.getsInstance().addEventHandler(mEventHandler)
    }
    
    override fun onCleared() {
        super.onCleared()
        ICPRtcManager.getsInstance().removeEventHandler(mEventHandler)
    }

    inner class aICPRtcEventHandler: ICPRtcEventHandler() {
        override fun onServiceConnected() {
            _state.value = "已连接"
            _connectionState.value = ConnectionState.CONNECTED
            if (ICPRtcManager.getsInstance().getCurrentUser()!= null) {
                _userName.value = ICPRtcManager.getsInstance().getCurrentUser().getName()
            }
            var a = 99
            GlobalScope.launch {
                while (a < 100){
                    a --
                    delay(1000)
                    if (ICPRtcManager.getsInstance().getCurrentUser() != null) {
                        GlobalScope.launch(Dispatchers.Main){
                            _userName.value = ICPRtcManager.getsInstance().getCurrentUser().getName()
                        }
                        break
                    }
                }
            }
        }

        override fun onServiceConnecting() {
            _state.value = "连接中"
            _connectionState.value = ConnectionState.CONNECTING
        }

        override fun onServiceConnectFailed(reason: Int) {
            _state.value = "连接失败"
            _connectionState.value = ConnectionState.DISCONNECTED
        }

        override fun onServiceDisconnected(reason: Int) {
            _state.value = "断开连接"
            _connectionState.value = ConnectionState.DISCONNECTED
        }

        override fun onNewConversationCreated(messageDialogue: MessageDialogue?) {
//            if (messageListFragment != null) {
//                messageListFragment.addNewConversation(messageDialogue);
//            }
        }

        override fun onPttStateEvent(sessionId: Long, channelId: String?, state: Int) {
            if (TextUtils.isEmpty(channelId)) {
                channelState.postValue("待加入对讲组")
            } else {
                //当前已经连接
                if (state == PttState.CONNECTED) {
                    val channel = ICPRtcManager.getsInstance().currentPttChannel
                    channelState.postValue(channel.channelName)
                } else if (state == PttState.CONNECTING) {
                    channelState.postValue("正在连接")
                } else {
                    channelState.postValue("未连接:")
                }
            }

        }
    }
}